<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends CommonController {
    public function index(){

    $question = D('verify')->getOneVerify() ;
    // D 是实例化模型的方法
    $this->assign('question',$question);
    //接受return回来的答案后分配到模板
     $this->display();
    }
    public function checkdata()
    {
        $verify = I('post.verify');
        if ($verify!=$_SESSION['answer']) {
            $arr = array(
            'error' => 1,
            'info'=>'人类验证错误'
            );
        echo json_encode($arr);
        //返回json格式
        exit;
        }

    if($res = D('user')->checkLogin(I('post.email'),I('post.password'))){

            $_SESSION['id']=$res['id'];
            $_SESSION['nickname']=$res['nickname'];
            $_SESSION['face']=$res['face'];

            $arr = array(
                'error'=>0,
                'info'=>'登录成功'
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>1,
                'info'=>'邮箱或者密码错误'
                );
            echo json_encode($arr);
            exit;
        }


    }

    public function loginout()
    {
        session_unset();
        session_destroy();

        $this->success('退出成功',U('home/index/index'),0);

    }



    /**
     * [qqreturn 回调]
     * @Author   Xuebingsi
     * @DateTime 2017-04-08T10:43:05+0800
     * @return   [type]                   [description]
     */
     public function qqreturn()
    {
        

        // Vendor('qqConnect.API.qqConnectAPI');

        // $qc = new \QC();//new一个类得到一个结果集
        // $qc->qq_callback();//回调
        // $openid=$qc->get_openid(); //获取openid 
        // var_dump($openid);


        // header("Content-type: text/html; charset=utf-8");

        //获取的openid = DAC94AFD560664C880790B354F1DCB4D

        $openid=getOpenid();

        // echo $openid;

        if($res = M('user')->where(array('openid'=>$openid))->find()){
            $_SESSION['id']=$res['id'];
            $_SESSION['nickname']=$res['nickname'];
            $_SESSION['face']=$res['face'];
            $this->success('登录成功',U('home/index/index'));
            //$openid已存在的，与数据库对照，有openid'=>$openid表明用户存在

        }else{

            // echo "wdfefefe";

            $userInfo = getUserInfo();

            // $data = file_get_contents($userInfo['figureurl_2']);

            // file_put_contents('./Upload/qqface/sdflsdfjl.jpg', $data);

            // echo "787878";
            
            $data['nickname']= $userInfo['nickname'];
            $data['openid']= $openid;
            $data['city']= $userInfo['city'];
            $data['create_time']= time();

            if($res = M('user')->add($data)){
                $_SESSION['id']=$res;
                $_SESSION['nickname']=$data['nickname'];
                // $_SESSION['face']=$res['face'];
                $this->success('登录成功',U('home/index/index'));
            }
        }
    }
    public function qqlogin()
    {
        // require_once("../../API/qqConnectAPI.php");
        // Vendor('QQConnect.API.qqConnectAPI');    //引第三方类
        // $qc = new \QC();
        // $qc->qq_login();
        xbslogin();
    }
}

// http://127.0.0.1/php/bbs/index.php?m=home&c=login&a=login
// http://127.0.0.1/php/bbs/index.php/home/login/login
// bbs-库index.php -单入口，home - 模块 ，login - 控制器，login- login里面的login方法






?>